package com.weiter.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiter.common.core.core.page.PageQuery;
import com.weiter.common.core.exception.ServiceException;
import com.weiter.system.domain.SysPost;
import com.weiter.system.domain.SysUserPost;
import com.weiter.system.mapper.SysPostMapper;
import com.weiter.system.mapper.SysUserPostMapper;
import com.weiter.system.service.ISysPostService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 岗位信息部门信息
 * @author weiter
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Resource
    private SysPostMapper postMapper;

    @Resource
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息分页列表
     * @param query     岗位信息查询条件
     * @param pageQuery 分页查询信息
     * @return 岗位信息页列表
     */
    @Override
    public Page<SysPost> selectPostPage(SysPost query, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPost> lqw = buildQueryWrapper(query);
        return postMapper.selectPage(pageQuery.build(), lqw);
    }

    /**
     * 查询岗位信息集合
     * @param query 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost query) {
        LambdaQueryWrapper<SysPost> lqw = buildQueryWrapper(query);
        return postMapper.selectList(lqw);
    }

    /**
     * 查询所有岗位信息
     * @return 所有岗位信息
     */
    @Override
    public List<SysPost> selectPostAll() {
        return postMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 查询岗位选择框列表
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否重复
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameDuplicate(SysPost post) {
        return postMapper.exists(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostName, post.getPostName())
                .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId()));
    }

    /**
     * 校验岗位编码是否重复
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeDuplicate(SysPost post) {
        return postMapper.exists(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostCode, post.getPostCode())
                .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId()));
    }

    /**
     * 批量删除岗位信息
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public boolean deletePostByIds(Long[] postIds) {
        for (Long postId : postIds) {
            Long used = userPostMapper.selectCount(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getPostId, postId));
            if (used > 0) {
                SysPost post = postMapper.selectById(postId);
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return this.removeByIds(Arrays.asList(postIds));
    }

    private LambdaQueryWrapper<SysPost> buildQueryWrapper(SysPost query) {
        LambdaQueryWrapper<SysPost> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(query.getPostCode()), SysPost::getPostCode, query.getPostCode());
        lqw.like(StrUtil.isNotBlank(query.getPostName()), SysPost::getPostName, query.getPostName());
        lqw.eq(StrUtil.isNotBlank(query.getStatus()), SysPost::getStatus, query.getStatus());
        lqw.orderByDesc(SysPost::getCreateTime);
        return lqw;
    }
}
